//
//  ITCollectionView.m
//  InTubePro
//
//  Created by seven on 2020/4/14.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITCollectionView.h"
#import "ITMJRefreshHeadGifImagesTool.h"

@implementation ITCollectionView


- (void)initViews{
    [super initViews];
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView setUserInteractionEnabled:YES];
    [_collectionView setBackgroundColor:[UIColor clearColor]];
    [_collectionView setShowsHorizontalScrollIndicator:NO];
    [_collectionView setShowsVerticalScrollIndicator:NO];
    //[_collectionView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self addSubview:_collectionView];
    //[_collectionView registerClass:[ITDiscoverVideoCell class] forCellWithReuseIdentifier:@"cell"];
    //[_contentView addSubview:_collectionView];
}

- (void)setupLayout{
    if (_itemSize.height ==0 && _itemSize.height==0) {
        int width = (kScreenWidth-5)/2.0;
        int height = 165.0/185.0*width;
        _itemSize = CGSizeMake(width, height);
    }
    _layout.itemSize = _itemSize;
    [_layout setScrollDirection:UICollectionViewScrollDirectionVertical];
}

- (UICollectionViewFlowLayout *)layout{
    if (_layout == nil) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        [self setupLayout];
    }
    return _layout;
}
- (void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [_collectionView reloadData];
    if (self.layout.scrollDirection == UICollectionViewScrollDirectionVertical){
        [self.collectionView scrollToTopAnimated:NO];
    }else {
        [self.collectionView scrollToLeftAnimated:NO];
    }
}
#pragma -mark UICollectionViewDataSource | UICollectionViewDelegateFlowLayout
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    ITTopicModel *model = [self.dataSource objectAtIndex:indexPath.row];
//    cell.model = model;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    ITTopicModel *model = [self.dataSource objectAtIndex:indexPath.row];
//    ITTopicController *controller = [ITTopicController new];
//    controller.topic = model;
//    [self.navigationController pushViewController:controller animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
        if (indexPath.row == _dataSource.count-1 && (indexPath.row%2)==0) {
            return CGSizeMake(collectionView.width, _itemSize.height);
        }
    }
    return _itemSize;
}


@end
